package com.mjl.usertest.controller;

import com.mjl.usertest.pojo.User;
import com.mjl.usertest.pojo.UserQuery;
import com.mjl.usertest.pojo.UserVo;
import com.mjl.usertest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/")
    public String getUsers(UserQuery userQuery, Model model) {
        List<User> users = userService.getUsers(userQuery);
        model.addAttribute("users",users);
        return "index";
    }

    @GetMapping("/login")
    public String login(Model model){
        model.addAttribute("login");
        return "login";
    }

    @PostMapping("/login")
    public String login(Model model, UserVo userVo){
        String name = userVo.getName();
        String pwd = userVo.getPwd();
        if (!userService.getUser(name)){
            if (userService.login(name).equals(pwd)){
                model.addAttribute("name",name);
                return "index";
            }
        }
        model.addAttribute("error","用户名或密码错误！");
        return "login";
    }
}
